include('shared.lua')

ENT.RenderGroup 		= RENDERGROUP_OPAQUE

function ENT:Think()
	local effectdata = EffectData() 
 		effectdata:SetOrigin(self.Entity:LocalToWorld(Vector(0,0,self.Entity:OBBMins().z))) 
 		effectdata:SetAngle(self.Entity:GetForward():Angle()) 
 		effectdata:SetScale(.5) 
 	util.Effect("MuzzleEffect", effectdata) 

	self.SmokeTimer = self.SmokeTimer or 0
	if (self.SmokeTimer > CurTime()) then return end
	
	self.SmokeTimer = CurTime() + 0.005

	local vOffset = self.Entity:LocalToWorld(Vector(0,0,self.Entity:OBBMins().z))

	local vNormal = (vOffset - self.Entity:GetPos()):GetNormalized()

	local emitter = ParticleEmitter(vOffset)
	
		local particle = emitter:Add("particles/smokey", vOffset)
			particle:SetVelocity(vNormal * math.Rand(10, 20))
			particle:SetDieTime(1.0)
			particle:SetStartAlpha(math.Rand(100, 150))
			particle:SetStartSize(math.Rand(5, 10))
			particle:SetEndSize(math.Rand(20, 50))
			particle:SetRoll(math.Rand(-0.2, 0.2))
			particle:SetColor(160, 160, 160)
				
	emitter:Finish()
end

function ENT:Initialize()
	if(self:GetOwner() == LocalPlayer()) then
		if timer.IsTimer("KillHooks") then 
			hook.Remove("HUDShouldDraw","HookKillHUD")
			hook.Remove("HUDPaint","HookHUD")
			hook.Remove("HUDPaintBackground","HookHUDDeadScreen")
			hook.Remove("RenderScreenspaceEffects","HookVision")
			timer.Destroy("KillHooks")
		end
		LocalPlayer().Rocket = self
		LocalPlayer().DontDrawHud = true
		HUDFuelEndTime = CurTime()+15
		hook.Add("HUDShouldDraw","HookKillHUD",KillHUD)
		hook.Add("HUDPaint","HookHUD",DrawMissileHUD)
		hook.Add("HUDPaint","HookHUDFuel",DrawFuelBar)
		hook.Add("HUDPaint","HookHUDMarkers",DrawMarkers)
		hook.Add("RenderScreenspaceEffects","HookVision",ColorModification)
	end
end

function ENT:OnRemove()
	if(self:GetOwner() == LocalPlayer()) then
	hook.Remove("HUDPaint","HookHUDFuel")
	hook.Remove("HUDPaint","HookHUDMarkers")
    hook.Add("HUDPaintBackground","HookHUDDeadScreen",DrawDeadScreen)
    timer.Create("KillHooks",1,1,function()
        hook.Remove("HUDShouldDraw","HookKillHUD")
        hook.Remove("HUDPaint","HookHUD")
        hook.Remove("HUDPaintBackground","HookHUDDeadScreen")
        hook.Remove("RenderScreenspaceEffects","HookVision")
		LocalPlayer().DontDrawHud = nil
    end)
	end
end

//I dont take credit for any of this, it came from SiPlus's rocket hud.
function ColorModification()
 
    local ScreenEffects = {}
    ScreenEffects[ "$pp_colour_addr" ] = 0
    ScreenEffects[ "$pp_colour_addg" ] = 0
    ScreenEffects[ "$pp_colour_addb" ] = 0
    ScreenEffects[ "$pp_colour_brightness" ] = 0
    ScreenEffects[ "$pp_colour_contrast" ] = 1
    ScreenEffects[ "$pp_colour_colour" ] = 0
    ScreenEffects[ "$pp_colour_mulr" ] = 0
    ScreenEffects[ "$pp_colour_mulg" ] = 0
    ScreenEffects[ "$pp_colour_mulb" ] = 0 
 
    DrawColorModify( ScreenEffects )
end

function DrawMissileHUD()
    surface.CreateFont("Arial Narrow",18,600,true,false,"ArialNarrow18",true,false)
    surface.SetDrawColor(191,191,191,255)
    --center
    surface.DrawOutlinedRect(ScrW()/2-28,ScrH()/2-28,56,56)
    surface.DrawLine(ScrW()/2,ScrH()/2-88,ScrW()/2,ScrH()/2-28)
    surface.DrawLine(ScrW()/2,ScrH()/2+28,ScrW()/2,ScrH()/2+88)
    surface.DrawLine(ScrW()/2-88,ScrH()/2,ScrW()/2-28,ScrH()/2)
    surface.DrawLine(ScrW()/2+28,ScrH()/2,ScrW()/2+88,ScrH()/2)
    --UL corner
    surface.DrawLine(ScrW()/2-192,ScrH()/2-192,ScrW()/2-136,ScrH()/2-192)
    surface.DrawLine(ScrW()/2-192,ScrH()/2-192,ScrW()/2-192,ScrH()/2-136)
    --UR corner
    surface.DrawLine(ScrW()/2+136,ScrH()/2-192,ScrW()/2+192,ScrH()/2-192)
    surface.DrawLine(ScrW()/2+192,ScrH()/2-192,ScrW()/2+192,ScrH()/2-136)
    --LL corner
    surface.DrawLine(ScrW()/2-192,ScrH()/2+192,ScrW()/2-136,ScrH()/2+192)
    surface.DrawLine(ScrW()/2-192,ScrH()/2+136,ScrW()/2-192,ScrH()/2+192)
    --LR corner
    surface.DrawLine(ScrW()/2+136,ScrH()/2+192,ScrW()/2+192,ScrH()/2+192)
    surface.DrawLine(ScrW()/2+192,ScrH()/2+136,ScrW()/2+192,ScrH()/2+192)

    --fuel dispenser icon
    surface.DrawLine(ScrW()/2+264,ScrH()/2-150,ScrW()/2+272,ScrH()/2-150)
    surface.DrawLine(ScrW()/2+264,ScrH()/2-150,ScrW()/2+264,ScrH()/2-144)
    surface.DrawLine(ScrW()/2+271,ScrH()/2-150,ScrW()/2+271,ScrH()/2-144)
    surface.DrawRect(ScrW()/2+264,ScrH()/2-144,8,12)
    surface.DrawLine(ScrW()/2+271,ScrH()/2-150,ScrW()/2+273,ScrH()/2-150)
    surface.DrawLine(ScrW()/2+273,ScrH()/2-150,ScrW()/2+277,ScrH()/2-144)
    surface.DrawLine(ScrW()/2+277,ScrH()/2-144,ScrW()/2+277,ScrH()/2-135)
    surface.DrawLine(ScrW()/2+273,ScrH()/2-136,ScrW()/2+275,ScrH()/2-134)
    surface.DrawLine(ScrW()/2+273,ScrH()/2-135,ScrW()/2+277,ScrH()/2-135)
    surface.DrawLine(ScrW()/2+273,ScrH()/2-142,ScrW()/2+273,ScrH()/2-134)
    surface.DrawLine(ScrW()/2+271,ScrH()/2-144,ScrW()/2+273,ScrH()/2-142)
    --text
    draw.SimpleText("Press MOUSE1 to detonate.","ArialNarrow18",64,ScrH()-74,Color(255,255,255,255),TEXT_ALIGN_LEFT,TEXT_ALIGN_CENTER)
    draw.SimpleText("Use WASD to control.","ArialNarrow18",64,ScrH()-48,Color(255,255,255,255),TEXT_ALIGN_LEFT,TEXT_ALIGN_CENTER)
    --fuel
    surface.SetDrawColor(191,191,191,191)
    surface.DrawRect(ScrW()/2+240,ScrH()/2-150,16,300)
end

function DrawFuelBar()
    local BarBottomLeftX,BarBottomLeftY = ScrW()/2+240,ScrH()/2+150
    if math.ceil((HUDFuelEndTime-CurTime())*10) >= 1 then
        surface.SetDrawColor(255-math.floor((HUDFuelEndTime-CurTime())*10)/150*255,math.floor((HUDFuelEndTime-CurTime())*10)/150*255,0,255)
        surface.DrawRect(BarBottomLeftX,BarBottomLeftY-math.ceil((HUDFuelEndTime-CurTime())*10)*2,16,math.ceil((HUDFuelEndTime-CurTime())*10)*2)
    end
end

function DrawMarkers()
    for _,allents in pairs(ents.GetAll()) do
        if (allents:IsPlayer()) or (allents.IsNPC()) then
            local entpos = allents:GetPos()+allents:OBBCenter()
			local tracedata = {}
			tracedata.start = LocalPlayer().Rocket:GetPos()
			tracedata.endpos = allents:GetPos()
			tracedata.filter = LocalPlayer().Rocket
			local trace = util.TraceLine(tracedata)
			if(trace.HitWorld == true || allents:IsPlayer() == false) then return end
			
            if (allents == LocalPlayer() || LocalPlayer():GetInfo("rpdm_ally_"..allents:UniqueID())==1) then
				surface.SetDrawColor(0,255,0,255)
				surface.DrawLine(entpos:ToScreen().x-12,entpos:ToScreen().y,entpos:ToScreen().x+12,entpos:ToScreen().y)
				surface.DrawLine(entpos:ToScreen().x,entpos:ToScreen().y-12,entpos:ToScreen().x,entpos:ToScreen().y+12)
            else
				surface.SetDrawColor(255,0,0,255)
				surface.DrawLine(entpos:ToScreen().x-12,entpos:ToScreen().y,entpos:ToScreen().x,entpos:ToScreen().y-12)
				surface.DrawLine(entpos:ToScreen().x,entpos:ToScreen().y-12,entpos:ToScreen().x+12,entpos:ToScreen().y)
				surface.DrawLine(entpos:ToScreen().x+12,entpos:ToScreen().y,entpos:ToScreen().x,entpos:ToScreen().y+12)
				surface.DrawLine(entpos:ToScreen().x,entpos:ToScreen().y+12,entpos:ToScreen().x-12,entpos:ToScreen().y)
            end
        end
    end
end

function DrawDeadScreen()
    surface.SetDrawColor(255,255,255,255)
    surface.SetTexture(surface.GetTextureID("effects/security_noise2"))
    surface.DrawTexturedRectUV(math.random(-128,0),math.random(-128,0),ScrW()+128,ScrH()+128,256,256)
end

function KillHUD(elem)
    if elem == "CHudWeaponSelection" or elem == "CHudSuitPower" or elem == "CHudHealth" or elem == "CHudBattery" or elem == "CHudAmmo" or elem == "CHudSecondaryAmmo" or elem == "CHudCrosshair" then return false end
end
